/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityEmploymentInformationInfo;
import org.kuali.rice.kns.util.KualiDecimal;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityEmploymentInformationInfoMapper implements ContextMapper {

  private String employeeIdAttributeName = null;
  private String employeeStatusCodeAttributeName = null;
  private String employeeTypeCodeAttributeName = null;
  private String employmentRecordIdAttributeName = null;
  private String entityAffiliationIdAttributeName = null;
  private String entityEmploymentIdAttributeName = null;
  private String primaryDepartmentCodeAttributeName = null;
//  private String baseSalaryAmountAttributeName = null;
  private final Log log = LogFactory.getLog(getClass());

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityEmploymentInformationInfo keeii = new KimEntityEmploymentInformationInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;

      keeii.setEmployeeId(LdapUtilities.materialize(context.getStringAttribute(employeeIdAttributeName)));
      keeii.setEmployeeStatusCode(LdapUtilities.materialize(context.getStringAttribute(employeeStatusCodeAttributeName)));
//      keeii.setEmployeeTypeCode(LdapUtilities.materialize(context.getStringAttribute(employeeTypeCodeAttributeName)));
      keeii.setEmployeeTypeCode("P");
      keeii.setEmploymentRecordId(LdapUtilities.materialize(context.getStringAttribute(employmentRecordIdAttributeName)));
      keeii.setEntityAffiliationId(LdapUtilities.materialize(context.getStringAttribute(entityAffiliationIdAttributeName)));
      keeii.setEntityEmploymentId(LdapUtilities.materialize(context.getStringAttribute(entityEmploymentIdAttributeName)));
      keeii.setPrimaryDepartmentCode(LdapUtilities.materialize(context.getStringAttribute(primaryDepartmentCodeAttributeName)));
//      keeii.setBaseSalaryAmount(LdapUtilities.materialize(context.getStringAttribute(baseSalaryAmountAttributeName)));
      keeii.setBaseSalaryAmount(new KualiDecimal(0));
      keeii.setPrimary(true);
      keeii.setActive(true); // is there an ldap attribute for this?

    } catch (Throwable t) {
      log.error("Error in KimEntityEmploymentInformationInfoMapper", t);
      String msg = "KimEntityEmploymentInformationInfoMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped LdapKimEntityEmploymentInformation: employeeId=" + keeii.getEmployeeId()
              + ", employeeStatusCode=" + keeii.getEmployeeStatusCode()
              + ", employeeTypeCode=" + keeii.getEmployeeTypeCode()
              + ", employmentRecordId=" + keeii.getEmploymentRecordId()
              + ", entityAffiliationId=" + keeii.getEntityAffiliationId()
              + ", entityEmploymentId=" + keeii.getEntityEmploymentId()
              + ", primaryDepartmentCode=" + keeii.getPrimaryDepartmentCode()
              + ", baseSalaryAmount=" + keeii.getBaseSalaryAmount()
              + ", isActive=" + keeii.isActive());
    }
    return keeii;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getEmployeeIdAttributeName() {
    return this.employeeIdAttributeName;
  }

  public void setEmployeeIdAttributeName(String attrName) {
    this.employeeIdAttributeName = attrName;
  }

  public String getEmployeeStatusCodeAttributeName() {
    return this.employeeStatusCodeAttributeName;
  }

  public void setEmployeeStatusCodeAttributeName(String attrName) {
    this.employeeStatusCodeAttributeName = attrName;
  }

  public String getEmployeeTypeCodeAttributeName() {
    return this.employeeTypeCodeAttributeName;
  }

  public void setEmployeeTypeCodeAttributeName(String attrName) {
    this.employeeTypeCodeAttributeName = attrName;
  }

  public String getEmploymentRecordIdAttributeName() {
    return this.employmentRecordIdAttributeName;
  }

  public void setEmploymentRecordIdAttributeName(String attrName) {
    this.employmentRecordIdAttributeName = attrName;
  }

  public String getEntityAffiliationIdAttributeName() {
    return this.entityAffiliationIdAttributeName;
  }

  public void setEntityAffiliationIdAttributeName(String attrName) {
    this.entityAffiliationIdAttributeName = attrName;
  }

  public String getEntityEmploymentIdAttributeName() {
    return this.entityEmploymentIdAttributeName;
  }

  public void setEntityEmploymentIdAttributeName(String attrName) {
    this.entityEmploymentIdAttributeName = attrName;
  }

  public String getPrimaryDepartmentCodeAttributeName() {
    return this.primaryDepartmentCodeAttributeName;
  }

  public void setPrimaryDepartmentCodeAttributeName(String attrName) {
    this.primaryDepartmentCodeAttributeName = attrName;
  }

//  public String getBaseSalaryAmountAttributeName() {
//    return this.baseSalaryAmountAttributeName;
//  }
//
//  public void setBaseSalaryAmountAttributeName(String attrName) {
//    this.baseSalaryAmountAttributeName = attrName;
//  }

  private void validateConfiguration() {
    if (employeeIdAttributeName == null) {
      String msg = "The property 'employeeIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (employeeStatusCodeAttributeName == null) {
      String msg = "The property 'employeeStatusCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (employeeTypeCodeAttributeName == null) {
      String msg = "The property 'employeeTypeCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (employmentRecordIdAttributeName == null) {
      String msg = "The property 'employmentRecordIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (entityAffiliationIdAttributeName == null) {
      String msg = "The property 'entityAffiliationIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (entityEmploymentIdAttributeName == null) {
      String msg = "The property 'entityEmploymentIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (primaryDepartmentCodeAttributeName == null) {
      String msg = "The property 'primaryDepartmentCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
//    if (baseSalaryAmountAttributeName == null) {
//      String msg = "The property 'baseSalaryAmountAttributeName' must be set.";
//      throw new IllegalStateException(msg);
//    }
  }
//end: helper methods
}
